﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HRDexCapstoneProject
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
        DataTable dtbl = new DataTable("dtbl");
        protected void Page_Load(object sender, EventArgs e)
        {
            string sqlcmd = "SELECT formEmployees.LastName + ', ' + formEmployees.FirstName+ ' ' + formEmployees.MiddleName AS FullName, catPositions.catPositionsName " + 
                "FROM formEmployees INNER JOIN catPositions ON formEmployees.catPositionsID = catPositions.catPositionsID" + 
                " WHERE formEmployees.EmployeeID = " + SessionID() ;

            try
            {
                SqlDataAdapter cmd = new SqlDataAdapter(sqlcmd, conn);
                conn.Open();
                cmd.Fill(dtbl);
                lblname.Text = dtbl.Rows[0].ItemArray[0].ToString();
                lblpos.Text = dtbl.Rows[0].ItemArray[1].ToString();

                if (dtbl.Rows[0].ItemArray[2].ToString() == "Employee")
                {
                    hDocuments.Visible = false;
                    hEmployees.Visible = false;
                    hRecruitment.Visible = false;
                }
                else
                {
                    
                }
            }
            catch (Exception ex)
            {
                //lblerror.Text = ex.ToString();
            }

        }
        
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            //this.cmdStore.Click += new System.EventHandler(this.cmdStore_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            Response.Cookies["name"].Expires = DateTime.Now.AddDays(-1);
            Server.Transfer("~/Default.aspx");
        }

        protected void btnMessage_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Functions/Messege/ReceivedItems.aspx?fromEmpID="+SessionID());
        }

        protected void btnRequest_Click(object sender, EventArgs e)
        {

            SqlCommand cmd2 = new SqlCommand("SELECT formEmployees.EmployeeID, catUserLevel.catUserLevelName FROM formEmployees INNER JOIN catUserLevel ON formEmployees.UserLevelID = catUserLevel.UserLevelID", conn);
            SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
            DataTable dt1 = new DataTable("dt1");
            da2.Fill(dt1);

            if (SessionID() != null)
            {
                if (dt1.Rows[0].ItemArray[1].ToString() == "Admin")
                {
                   
                    Response.Redirect("~/Functions/Forms/Main.aspx");
                }
                else if ( dt1.Rows[0].ItemArray[1].ToString() == "SubAdmin")
                {
                     Response.Redirect("~/Functions/Forms/Main.aspx");
                }
                else
                {
                    Response.Redirect("~/Functions/Forms/Main.aspx?EmployeeID=" + SessionID());
                }

            }
 
            
        }

    }
        
}
